<template>
  <div class="roadmap-container">
    <div class="roadmap-wrapper">
      <!-- Header Section -->
      <div class="header-section">
        <h1 class="main-title">您的个人提升路线规划</h1>
        <p class="main-description">基于您的面试表现分析，我们为您制定了个性化的提升计划</p>

        <!-- Loading State -->
        <div v-if="loading" class="loading-section">
          <loading-spinner text="正在加载您的提升路线……"/>
        </div>

        <!-- 提升总览图 -->
        <div v-else-if="roadmapSteps.length > 0" class="overview-section">
          <div class="overview-chart">
            <div class="overview-steps">
              <div
                  v-for="(step, index) in roadmapSteps"
                  :key="step.id"
                  class="overview-step"
                  :class="{ 'overview-step-active': currentStepIndex === index }"
                  @click="goToStep(index)"
              >
                <div class="overview-step-number">{{ step.id }}</div>
                <div class="overview-step-title">{{ step.title }}</div>
                <div class="overview-step-icon">
                  <component :is="step.icon" class="overview-icon" />
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>

      <!-- 书本翻页区域 -->
      <div v-if="!loading && !error && roadmapSteps.length > 0" class="book-section">
        <div class="book-container">
          <div class="book-navigation">
            <button
                class="nav-btn nav-btn-prev"
                @click="previousStep"
                :disabled="currentStepIndex === 0"
            >
              <ChevronLeft class="nav-icon" />
              上一步
            </button>

            <div class="page-indicator">
              第 {{ currentStepIndex + 1 }} 步 / 共 {{ totalSteps }} 步
            </div>

            <button
                class="nav-btn nav-btn-next"
                @click="nextStep"
                :disabled="currentStepIndex === totalSteps - 1"
            >
              下一步
              <ChevronRight class="nav-icon" />
            </button>
          </div>

          <div class="book-wrapper">
            <div class="book-pages">
              <transition
                  :name="transitionName"
                  mode="out-in"
                  @before-enter="onBeforeEnter"
                  @enter="onEnter"
                  @leave="onLeave"
              >
                <div
                    :key="currentStepIndex"
                    class="book-page"
                >
                  <div class="page-content">
                    <div class="page-header">
                      <div class="page-step-info">
                        <div class="page-step-icon" :class="getIconContainerClass(currentStep.color)">
                          <component :is="currentStep.icon" :class="getIconClass(currentStep.color)" />
                        </div>
                        <div class="page-step-details">
                          <h3 class="page-step-title">
                            第{{ currentStep.id }}步：{{ currentStep.title }}
                          </h3>
                          <p class="page-step-description">{{ currentStep.description }}</p>
                        </div>
                      </div>

                      <div class="page-badges">
                        <div class="duration-badge">
                          <Clock class="duration-icon" />
                          <span>预计时间：{{ currentStep.duration }}</span>
                        </div>
                        <div :class="getPriorityBadgeClass(currentStep.priority)">
                          <span class="priority-icon">{{ getPriorityIcon(currentStep.priority) }}</span>
                          <span>{{ currentStep.priority }}优先级</span>
                        </div>
                      </div>
                    </div>

                    <div class="page-body">
                      <div class="tasks-section">
                        <h4 class="tasks-title">
                          <Target class="tasks-icon" />
                          具体任务
                        </h4>
                        <div class="tasks-grid">
                          <div
                              v-for="(task, taskIndex) in currentStep.tasks"
                              :key="taskIndex"
                              class="task-card"
                          >
                            <div class="task-number">{{ taskIndex + 1 }}</div>
                            <div class="task-content">{{ task }}</div>
                          </div>
                        </div>
                      </div>
                    </div>
                  </div>
                </div>
              </transition>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { ref, computed, onMounted } from 'vue'
import {
  Clock,
  Target,
  BookOpen,
  Users,
  Lightbulb,
  TrendingUp,
  ChevronLeft,
  ChevronRight,
  FileText,
  Video,
  Link
} from 'lucide-vue-next'
import {getImprovementPlan} from "@/http/m_api/interview";
import {useRoute} from "vue-router";
import LoadingSpinner from "@/components/common/LoadingSpinner.vue";

// 响应式数据
const currentStepIndex = ref(0)
const transitionName = ref('slide-left')
const roadmapSteps = ref([])
const loading = ref(true)
const error = ref(null)

const route = useRoute();

const resumeId = Number.parseInt(route.params.id as string)

// 图标和颜色映射配置
const iconMapping = [
  { icon: BookOpen, color: "blue" },
  { icon: Users, color: "green" },
  { icon: Target, color: "purple" },
  { icon: Lightbulb, color: "orange" },
  { icon: TrendingUp, color: "red" },
  { icon: FileText, color: "indigo" },
  { icon: Video, color: "pink" },
  { icon: Link, color: "yellow" }
]


// 计算属性
const currentStep = computed(() => roadmapSteps.value[currentStepIndex.value])
const totalSteps = computed(() => roadmapSteps.value.length)

// 导航方法
const nextStep = () => {
  if (currentStepIndex.value < roadmapSteps.value.length - 1) {
    transitionName.value = 'slide-left'
    currentStepIndex.value++
  }
}

const previousStep = () => {
  if (currentStepIndex.value > 0) {
    transitionName.value = 'slide-right'
    currentStepIndex.value--
  }
}

const goToStep = (index) => {
  if (index !== currentStepIndex.value) {
    transitionName.value = index > currentStepIndex.value ? 'slide-left' : 'slide-right'
    currentStepIndex.value = index
  }
}

// 动画方法
const onBeforeEnter = (el) => {
  el.style.opacity = '0'
}

const onEnter = (el) => {
  el.offsetHeight // 触发重排
  el.style.transition = 'opacity 0.5s ease'
  el.style.opacity = '1'
}

const onLeave = (el) => {
  el.style.transition = 'opacity 0.3s ease'
  el.style.opacity = '0'
}

// 样式类方法
const getPriorityBadgeClass = (priority) => {
  switch (priority) {
    case "高":
      return "badge badge-high-priority"
    case "中":
      return "badge badge-medium-priority"
    default:
      return "badge badge-low-priority"
  }
}

const getIconContainerClass = (color) => {
  return `step-icon-${color}`
}

const getIconClass = (color) => {
  return `step-icon-svg step-icon-svg-${color}`
}

const getPriorityIcon = (priority) => {
  switch (priority) {
    case "高":
      return "🔥"
    case "中":
      return "⚡"
    default:
      return "📝"
  }
}

// 组件挂载时获取数据
onMounted(async() => {
  try {
    loading.value = true
    error.value = null

    const response = await getImprovementPlan(resumeId)

    const data = await response.data

    // 处理后端数据，添加图标和颜色
    roadmapSteps.value = data.plans.map((step, index) => ({
      ...step,
      id: index + 1, // 如果后端没有提供id，自动生成
      icon: iconMapping[index % iconMapping.length].icon,
      color: iconMapping[index % iconMapping.length].color
    }))

  } catch (err) {
    console.error('获取路线图数据失败:', err)
    error.value = err.message || '获取数据失败，请稍后重试'
  } finally {
    loading.value = false
  }
})
</script>

<style scoped lang="scss">
/* 基础容器样式 */
.roadmap-container {
  min-height: 100vh;
  background: linear-gradient(135deg, #f8fafc 0%, #e0f2fe 50%, #e8f4f8 100%);
  padding: 1rem;
}

@media (min-width: 768px) {
  .roadmap-container {
    padding-top: 15px;
    padding-bottom: 0;
  }
}

.roadmap-wrapper {
  max-width: 64rem;
  margin: 0 auto;
}

/* 头部样式 */
.header-section {
  text-align: center;
  margin-bottom: 0;
}

.loading-section, .error-section {
  text-align: center;
  padding: 2rem;
  margin: 2rem 0;
}

.main-title {
  font-size: 1.875rem;
  font-weight: 700;
  color: #111827;
  margin-bottom: 1rem;
}

@media (min-width: 768px) {
  .main-title {
    font-size: 2.25rem;
  }
}

.main-description {
  font-size: 1.125rem;
  color: #4b5563;
  margin-bottom: 1.5rem;
}

/* 提升总览图样式 */
.overview-section {
  margin-bottom: 2rem;
}

.overview-chart {
  background: white;
  border-radius: 1rem;
  padding: 2rem;
  box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
}

.overview-steps {
  display: flex;
  justify-content: space-between;
  gap: 1rem;
  flex-wrap: wrap;
}

@media (max-width: 768px) {
  .overview-steps {
    flex-direction: column;
    gap: 0.75rem;
  }
}

.overview-step {
  flex: 1;
  min-width: 0;
  background: #f8fafc;
  border: 2px solid #e2e8f0;
  border-radius: 0.75rem;
  padding: 1rem;
  text-align: center;
  cursor: pointer;
  transition: all 0.3s ease;
  position: relative;
}

.overview-step:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

.overview-step-active {
  border-color: #3b82f6;
  background: #eff6ff;
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(59, 130, 246, 0.2);
}

.overview-step-number {
  width: 2rem;
  height: 2rem;
  border-radius: 50%;
  background: #2563eb;
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 600;
  margin: 0 auto 0.5rem;
  font-size: 0.875rem;
}

.overview-step-title {
  font-size: 0.875rem;
  font-weight: 500;
  color: #374151;
  margin-bottom: 0.5rem;
  line-height: 1.2;
}

.overview-step-icon {
  display: flex;
  justify-content: center;
}

.overview-icon {
  width: 1.25rem;
  height: 1.25rem;
  color: #6b7280;
}

/* 书本翻页区域样式 */
.book-section {
  margin-bottom: 3rem;
}

.book-container {
  background: white;
  border-radius: 1rem;
  box-shadow: 0 10px 25px rgba(0, 0, 0, 0.1);
  overflow: hidden;
}

.book-navigation {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 1rem 2rem;
  background: #f8fafc;
  border-bottom: 1px solid #e2e8f0;
}

.nav-btn {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.5rem 1rem;
  border: 1px solid #d1d5db;
  border-radius: 0.5rem;
  background: white;
  color: #374151;
  font-size: 0.875rem;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.2s ease;
}

.nav-btn:hover:not(:disabled) {
  background: #f3f4f6;
  border-color: #9ca3af;
}

.nav-btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.nav-icon {
  width: 1rem;
  height: 1rem;
}

.page-indicator {
  font-size: 0.875rem;
  font-weight: 500;
  color: #6b7280;
}

.book-wrapper {
  position: relative;
  height: 50vh;
  overflow: hidden;
}

.book-pages {
  position: relative;
  width: 100%;
  height: 100%;
}

.book-page {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: white;
}

/* 翻页动画 */
.slide-left-enter-active,
.slide-left-leave-active,
.slide-right-enter-active,
.slide-right-leave-active {
  transition: all 0.5s cubic-bezier(0.25, 0.46, 0.45, 0.94);
}

.slide-left-enter-from {
  transform: translateX(100%);
  opacity: 0;
}

.slide-left-leave-to {
  transform: translateX(-100%);
  opacity: 0;
}

.slide-right-enter-from {
  transform: translateX(-100%);
  opacity: 0;
}

.slide-right-leave-to {
  transform: translateX(100%);
  opacity: 0;
}

/* 页面内容样式 */
.page-content {
  padding: 2rem;
  height: 100%;
  display: flex;
  flex-direction: column;
}

.page-header {
  margin-bottom: 2rem;
}

.page-step-info {
  display: flex;
  align-items: flex-start;
  gap: 1rem;
  margin-bottom: 1rem;
}

.page-step-icon {
  padding: 0.75rem;
  border-radius: 0.75rem;
  flex-shrink: 0;
}

.step-icon-blue {
  background-color: rgba(59, 130, 246, 0.1);
}

.step-icon-green {
  background-color: rgba(34, 197, 94, 0.1);
}

.step-icon-purple {
  background-color: rgba(147, 51, 234, 0.1);
}

.step-icon-orange {
  background-color: rgba(249, 115, 22, 0.1);
}

.step-icon-red {
  background-color: rgba(239, 68, 68, 0.1);
}

.step-icon-svg {
  width: 2rem;
  height: 2rem;
}

.step-icon-svg-blue {
  color: #3b82f6;
}

.step-icon-svg-green {
  color: #22c55e;
}

.step-icon-svg-purple {
  color: #9333ea;
}

.step-icon-svg-orange {
  color: #f97316;
}

.step-icon-svg-red {
  color: #ef4444;
}

.page-step-details {
  flex: 1;
}

.page-step-title {
  font-size: 1.5rem;
  font-weight: 600;
  color: #111827;
  margin: 0 0 0.5rem 0;
}

.page-step-description {
  font-size: 1rem;
  color: #6b7280;
  margin: 0;
  line-height: 1.5;
}

.page-badges {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
}

/* 徽章样式 */
.badge {
  display: inline-flex;
  align-items: center;
  padding: 0.25rem 0.75rem;
  border-radius: 9999px;
  font-size: 0.75rem;
  font-weight: 500;
}

.badge-high-priority {
  background-color: #fee2e2;
  color: #991b1b;
}

.badge-medium-priority {
  background-color: #fef3c7;
  color: #92400e;
}

.badge-low-priority {
  background-color: #f3f4f6;
  color: #374151;
}

.duration-badge {
  background: linear-gradient(135deg, #eff6ff 0%, #dbeafe 100%);
  color: #1e40af;
  border: 1px solid #93c5fd;
  display: inline-flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.5rem 1rem;
  border-radius: 1rem;
  font-size: 0.875rem;
  font-weight: 600;
}

.duration-icon {
  width: 1rem;
  height: 1rem;
}

.priority-icon {
  font-size: 1rem;
}

/* 页面主体样式 */
.page-body {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 2rem;
}

.tasks-section {
  flex: 1;
}

.tasks-title {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  font-size: 1.125rem;
  font-weight: 600;
  color: #111827;
  margin: 0 0 1rem 0;
}

.tasks-icon {
  width: 1.25rem;
  height: 1.25rem;
  color: #3b82f6;
}

.tasks-grid {
  display: grid;
  gap: 0.75rem;
}

.task-card {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 1rem;
  background: #f8fafc;
  border: 1px solid #e2e8f0;
  border-radius: 0.5rem;
  transition: all 0.2s ease;
}

.task-card:hover {
  background: #f1f5f9;
  border-color: #cbd5e1;
}

.task-number {
  width: 1.5rem;
  height: 1.5rem;
  border-radius: 50%;
  background: #3b82f6;
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.75rem;
  font-weight: 600;
  flex-shrink: 0;
}

.task-content {
  font-size: 0.875rem;
  color: #374151;
  line-height: 1.4;
}

/* 响应式调整 */
@media (max-width: 767px) {
  .page-content {
    padding: 1rem;
  }

  .page-step-info {
    flex-direction: column;
    align-items: flex-start;
    gap: 0.75rem;
  }

  .book-navigation {
    padding: 1rem;
    flex-direction: column;
    gap: 1rem;
  }

  .nav-btn {
    width: 100%;
    justify-content: center;
  }
}
</style>